/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Vendor;
import org.crystalrange.enterprise.service.VendorService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class VendorListController extends BaseController {

    protected VendorService vendorService;
    protected Vendor selectedVendor;
    protected Listbox listboxVendor;
    protected Search searchVendor;
    protected List<Vendor> vendors;

    public VendorService getVendorService() {
        return this.vendorService;
    }

    public void setVendorService(VendorService vendorService) {
        this.vendorService = vendorService;
    }

    public Vendor getSelectedVendor() {
        return this.selectedVendor;
    }

    public void setSelectedVendor(Vendor selectedVendor) {
        this.selectedVendor = selectedVendor;
    }

    public List<Vendor> getVendors() {
        return this.vendors;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchVendor = new Search(Vendor.class);
        this.searchVendor.setMaxResults(100);
        this.searchVendor.addFetch("createdBy");
        this.searchVendor.addFetch("updatedBy");
        this.vendors = this.vendorService.search(this.searchVendor);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemVendorCreate(Event event) throws Exception {
        this.vendorCreate();
    }

    public void onClick$menuitemVendorEdit(Event event) throws Exception {
        this.vendorEdit();
    }

    public void onClick$menuitemVendorDelete(Event event) throws Exception {
        this.vendorDelete();
    }

    public void onClick$menuitemVendorSearch(Event event) throws Exception {
        this.vendorSearch();
    }

    public void onClick$menuitemVendorRefresh(Event event) throws Exception {
        this.vendorRefresh();
    }

    public void onClick$toolbarbuttonVendorCreate(Event event) throws Exception {
        this.vendorCreate();
    }

    public void onClick$toolbarbuttonVendorEdit(Event event) throws Exception {
        this.vendorEdit();
    }

    public void onClick$toolbarbuttonVendorDelete(Event event) throws Exception {
        this.vendorDelete();
    }

    public void onClick$toolbarbuttonVendorSearch(Event event) throws Exception {
        this.vendorSearch();
    }

    public void onClick$toolbarbuttonVendorRefresh(Event event) throws Exception {
        this.vendorRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.vendorEdit();
    }
    
    private void vendorCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/vendor.zul");
    }

    private void vendorEdit() throws Exception {
        if (this.selectedVendor != null) {
            this.execution.sendRedirect("~./zul/html/vendor.zul?Id=" + this.selectedVendor.getId().toString());
        }
    }

    private void vendorDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxVendor.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Vendor vendor = (Vendor) selectedItem.getValue();
                    this.vendorService.remove(vendor);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.vendors = this.vendorService.search(this.searchVendor);
        this.binder.loadAll();
    }

    private void vendorSearch() throws Exception {
        this.execution.setAttribute("searchVendor", this.searchVendor);

        Window window = (Window) this.execution.createComponents("~./zul/html/vendorSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchVendor = (Search) this.execution.getAttribute("searchVendor");
        }

        this.vendors = this.vendorService.search(this.searchVendor);
        this.binder.loadAll();
    }

    private void vendorRefresh() throws Exception {
        this.vendors = this.vendorService.search(this.searchVendor);
        this.binder.loadAll();
    }
}
